//
//  ZWVisitorView.swift
//  ZWWeibo
//
//  Created by flyiron on 16/4/16.
//  Copyright © 2016年 flyiron. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol ZWVisitorViewDelegate:NSObjectProtocol {
    func clickRegister()
    func clickLogin()
}

class ZWVisitorView: UIView {

    weak var delegate:ZWVisitorViewDelegate?
    
    
    
   override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
    func setValueForView(messageInfo: String? , iconImageName: String?) {
        if let message = messageInfo , let icon = iconImageName {
            messageLabel.text = message
            iconImageView.image = UIImage(named: icon)
            circleImageView.hidden = true
        }else{
            circleAnimation()
        }
    }
    
    private func circleAnimation(){
        let anima = CABasicAnimation()
        anima.duration = 10
        anima.repeatCount = MAXFLOAT
        anima.keyPath = "transform.rotation"
        anima.toValue = 2 * M_PI
        anima.removedOnCompletion = false
        circleImageView.layer.addAnimation(anima, forKey: nil)
        
    }
    
    func setupUI() {
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        //添加控件
        addSubview(circleImageView)
        addSubview(maskIconImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerBTN)
        addSubview(loginBTN)
        
        //设置约束
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        circleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.top.equalTo(circleImageView.snp_bottom).offset(15)
        }
        
        registerBTN.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(15)
            make.width.equalTo(100)
        }
        loginBTN.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(messageLabel.snp_bottom).offset(15)
            make.width.equalTo(100)
        }
        
        maskIconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
    }
    
    private lazy var iconImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var circleImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        label.text = "外表看似小孩智商却低于小孩的老王👴🏿👴🏿👴🏿👴🏿"
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = 230
        label.textAlignment = .Center
        label.font = UIFont.systemFontOfSize(14)
        return label
    }()
    private lazy var registerBTN:UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.addTarget(self, action: #selector(ZWVisitorView.clickRegisterBTN), forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    private lazy var loginBTN:UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        button.setTitle("登录", forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: #selector(ZWVisitorView.clickloginBTN), forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    func clickRegisterBTN(){
        delegate?.clickRegister()
    }
    
    func clickloginBTN(){
        delegate?.clickLogin()
    }
    private lazy var maskIconImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
}


extension ZWVisitorView{
func setupUI2() {
    backgroundColor = UIColor(white: 237/255.0, alpha: 1)
    //添加控件
    addSubview(circleImageView)
    addSubview(maskIconImageView)
    addSubview(iconImageView)
    addSubview(messageLabel)
    addSubview(registerBTN)
    addSubview(loginBTN)
    
    //设置约束
    iconImageView.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(self)
    }
    iconImageView.translatesAutoresizingMaskIntoConstraints = false
    addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    
    addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
    
    
    
    circleImageView.translatesAutoresizingMaskIntoConstraints = false
    addConstraint(NSLayoutConstraint(item: circleImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: circleImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
    
    messageLabel.translatesAutoresizingMaskIntoConstraints = false
    addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    
    addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: circleImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 15))
    addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1, constant: 230))
    
    registerBTN.translatesAutoresizingMaskIntoConstraints = false
    addConstraint(NSLayoutConstraint(item: registerBTN, attribute: .Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: registerBTN, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 15))
    addConstraint(NSLayoutConstraint(item: registerBTN, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1, constant: 100))
    
    loginBTN.translatesAutoresizingMaskIntoConstraints = false
    addConstraint(NSLayoutConstraint(item: loginBTN, attribute: NSLayoutAttribute.Trailing, relatedBy: .Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: loginBTN, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 15))
    addConstraint(NSLayoutConstraint(item: loginBTN, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1, constant: 100))
    
    
    maskIconImageView.translatesAutoresizingMaskIntoConstraints = false
    addConstraint(NSLayoutConstraint(item: maskIconImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    
    addConstraint(NSLayoutConstraint(item: maskIconImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
}
}
